package edu.jptest.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import edu.jptest.bean.Answer;
import edu.jptest.bean.Question;
import edu.jptest.bean.QuestionContent;
import edu.jptest.bean.QuestionOrigin;
import edu.jptest.bean.QuestionSubject;
import edu.jptest.bean.QuestionType;
import edu.jptest.bean.TestPoint;
import edu.jptest.service.IAnswerService;
import edu.jptest.service.IQuestionContentService;
import edu.jptest.service.IQuestionOriginService;
import edu.jptest.service.IQuestionService;
import edu.jptest.service.IQuestionSubjectService;
import edu.jptest.service.IQuestionTypeService;
import edu.jptest.service.ITestPointService;
import edu.jptest.serviceImp.AnswerService;
import edu.jptest.serviceImp.QuestionContentService;
import edu.jptest.serviceImp.QuestionOriginService;
import edu.jptest.serviceImp.QuestionService;
import edu.jptest.serviceImp.QuestionSubjectService;
import edu.jptest.serviceImp.QuestionTypeService;
import edu.jptest.serviceImp.TestPointService;

public class QuestionAction extends ActionSupport {
	private static final long serialVersionUID = -8110880790381928585L;

	private Question question;
	private List<Question> questionList;
	private QuestionOrigin questionOrigin;
	private QuestionType questionType;
	private QuestionSubject questionSubject;
	private QuestionContent questionContent;
	private Answer answer;
	private TestPoint testPoint;

	public List<Question> getQuestionList() {
		return questionList;
	}

	public void setQuestionList(List<Question> questionList) {
		this.questionList = questionList;
	}

	public TestPoint getTestPoint() {
		return testPoint;
	}

	public void setTestPoint(TestPoint testPoint) {
		this.testPoint = testPoint;
	}

	public Answer getAnswer() {
		return answer;
	}

	public void setAnswer(Answer answer) {
		this.answer = answer;
	}

	public QuestionContent getQuestionContent() {
		return questionContent;
	}

	public void setQuestionContent(QuestionContent questionContent) {
		this.questionContent = questionContent;
	}

	public QuestionSubject getQuestionSubject() {
		return questionSubject;
	}

	public void setQuestionSubject(QuestionSubject questionSubject) {
		this.questionSubject = questionSubject;
	}

	public QuestionType getQuestionType() {
		return questionType;
	}

	public void setQuestionType(QuestionType questionType) {
		this.questionType = questionType;
	}

	public QuestionOrigin getQuestionOrigin() {
		return questionOrigin;
	}

	public void setQuestionOrigin(QuestionOrigin questionOrigin) {
		this.questionOrigin = questionOrigin;
	}

	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	// 选择题型后添加试题
	@SuppressWarnings("unchecked")
	public String addQuestion() {
		IQuestionOriginService iOriginService = new QuestionOriginService();
		Map session = ActionContext.getContext().getSession();
		String bookName = (String) session.get("bookName");
		if (bookName == null) {
			return INPUT;
		}
		int lesson = 0;
		if (null == questionOrigin) {
			lesson = (Integer) session.get("lesson");
			if (session.get("lesson") == null) {
				System.out
						.println("QuestionAction.addQuestion()错误：lesson in session is null!!!!");
			}
			int originId = iOriginService
					.getQuestionOriginIdByBookNameAndLesson(bookName, lesson);
			questionOrigin = new QuestionOrigin();
			questionOrigin.setQuestionOriginId(originId);
			questionOrigin.setTextbookName(bookName);
			questionOrigin.setLesson(lesson);
		} else {
			lesson = questionOrigin.getLesson();
			session.put("lesson", lesson);
			int originId = iOriginService
					.getQuestionOriginIdByBookNameAndLesson(bookName, lesson);
			questionOrigin.setQuestionOriginId(originId);
		}

		String type = "", content = "";
		IQuestionTypeService iQuestionTypeService = new QuestionTypeService();
		if (null == questionType) {
			type = (String) session.get("type");
			content = (String) session.get("content");
			if (session.get("lesson") == null) {
				System.out
						.println("QuestionAction.addQuestion()错误：lesson in session is null!!!!");
			}
			int typeId = iQuestionTypeService.getQuestionTypeIdByContent(type,
					content);
			questionType = new QuestionType();
			questionType.setQuestionTypeId(typeId);
			questionType.setType(type);
			questionType.setContent(content);
		} else {
			type = questionType.getType();
			content = questionType.getContent();

			session.put("type", type);
			session.put("content", content);
			int typeId = iQuestionTypeService.getQuestionTypeIdByContent(type,
					content);
			questionType.setQuestionTypeId(typeId);
		}

		if (session.get("questionSubjectContent") != null
				&& session.get("questionBookNum") != null
				&& session.get("questionContent") != null) {
			String questionSubjectContent = "";
			int questionBookNum = 0;
			String questioncontent = "";
			questionBookNum = (Integer) session.get("questionBookNum");
			questioncontent = (String) session.get("questionContent");
			questionSubjectContent = (String) session
					.get("questionSubjectContent");
			questionSubject = new QuestionSubject();
			questionContent = new QuestionContent();
			questionSubject.setQuestionSubjectContent(questionSubjectContent);
			questionSubject.setQuestionBookNum(questionBookNum);
			questionContent.setQuestionContent(questioncontent);
		}

		return SUCCESS;
	}

	// 插入新题
	@SuppressWarnings("unchecked")
	public String insertQuestion() {
		IQuestionService iQuestionService = new QuestionService();
		question.setQuestionOriginId(questionOrigin.getQuestionOriginId());
		question.setQuestionTypeId(questionType.getQuestionTypeId());
		question.setInsertTime(new Date());

		Map session = ActionContext.getContext().getSession();
		session.put("questionSubjectContent", questionSubject
				.getQuestionSubjectContent());
		session.put("questionBookNum", questionSubject.getQuestionBookNum());
		session.put("questionContent", questionContent.getQuestionContent());

		question.setUserId(5);// 从session中取出userId

		ITestPointService iTestPointService = new TestPointService();
		int testPointId = iTestPointService.getTestPointIdByContent(testPoint
				.getType(), testPoint.getContent());

		System.out.println("testPoint ID: " + testPointId);
		question.setTestPointId(testPointId);

		if (iQuestionService.insertQuestion(question, questionSubject,
				questionContent, answer)) {
			return Action.SUCCESS;
		} else {
			return Action.INPUT;
		}
	}

	// 得到某一教材的试题列表
	public String getQuestionsByOrigin() {
		IQuestionService iQuestionService = new QuestionService();
		IQuestionOriginService iOriginService = new QuestionOriginService();
		questionList=new ArrayList<Question>();
		
		int origin_id = iOriginService.getQuestionOriginIdByBookNameAndLesson(
				questionOrigin.getTextbookName(), questionOrigin.getLesson());
		List<Integer> questionIdList = iQuestionService
				.getQuestionsIdByOriginId(origin_id);
		Iterator<Integer> it = questionIdList.iterator();
		while (it.hasNext()) {
			Question ques = new Question();
			int questionId = (Integer) it.next();
			ques = iQuestionService.getQuestionByQuestionId(questionId);
			questionList.add(ques);
		}
		return Action.SUCCESS;
	}

	// 删除试题
	public String deleteQuestion() {
		IQuestionService iQuestionService = new QuestionService();
		if (iQuestionService.deleteQuestion(question.getQuestionId()) == true) {
			return SUCCESS;
		} else {
			return INPUT;
		}
	}

	// 修改试题
	public String modifyQuestionInfo() {
		IQuestionService iQuestionService = new QuestionService();
		question = iQuestionService.getQuestionByQuestionId(question
				.getQuestionId());
		IAnswerService iAnswerService = new AnswerService();
		answer = iAnswerService.getAnswerByAnswerId(question.getAnswerId());

		if (question.getQuestionContentId() != 0) {
			IQuestionContentService iContentService = new QuestionContentService();
			questionContent = iContentService
					.getQuestionContentByQuestionContentId(question
							.getQuestionContentId());
		}

		IQuestionSubjectService iQuestionSubjectService = new QuestionSubjectService();
		questionSubject = iQuestionSubjectService
				.getQuestionSubjectByQuestionSubjectId(question
						.getQuestionSubjectId());

		IQuestionTypeService iQuestionTypeService = new QuestionTypeService();
		questionType = iQuestionTypeService
				.getquestionTypeByquestionTypeId(question.getQuestionTypeId());
		return SUCCESS;
	}

	// 更新试题
	public String updateQuestion() {
		IQuestionService iQuestionService = new QuestionService();
		Question questionDemo = iQuestionService
				.getQuestionByQuestionId(question.getQuestionId());
		questionDemo.setQuestionChild(question.getQuestionChild());
		questionDemo.setQuestionNumber(question.getQuestionNumber());
		iQuestionService.updateQuestion(questionDemo, answer, questionContent,
				questionSubject);
		return SUCCESS;
	}
}
